package fr.utbm.gi.in56.ads;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import fr.utbm.gi.in56.database.ConnectionPool;
import fr.utbm.gi.in56.message.MessageManager;

/**
 * Thread that run a matching with a given buy ad
 *
 * @author Adrien Hoffer & Tarik Belabbas
 *
 */
public class BuyAdMatcher extends Thread
{
	
	/**
	 * A buy ad
	 */
	private AdBuy 			adBuy;
	
	/**
	 * A connection pool
	 */
	private ConnectionPool 	connectionPool;
	
	/**
	 * The user Id
	 */
	private String 			userId;
	
	/**
	 * CConstructor
	 * 
	 * @param _adBuy the buy ad
	 */
	public BuyAdMatcher( AdBuy _adBuy, ConnectionPool _connectionPool,
			String _userId)
	{
		this.adBuy 		= _adBuy;
		this.connectionPool = _connectionPool;
		this.userId			= _userId ;
		
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run()
	{
		/**
		 *  Check if there is some matching for the ad and returns max 5 matches
		 *  Send a message to each buy ads owner which match with this add
		 */
		Connection connection = this.connectionPool.getConnection();
		AdsManager adsMg = new AdsManager( connection );
		List< AdSell > adSellList = new LinkedList< AdSell >();
		adSellList = adsMg.getSellAds( this.adBuy.getIdBrand(),
				this.adBuy.getIdModel(), this.adBuy.isAutomatic(),
				this.adBuy.getPriceMin(), this.adBuy.getPriceMax(),
				this.adBuy.getVehicleYearMin(), this.adBuy.getVehicleYearMax(),
				this.adBuy.getKmNbMin(), this.adBuy.getKmNbMax(),
				this.adBuy.getIdEnergy(), 0, 0, false, AdState.OPENED.ordinal(), -1, 0, "", "" );
		
		if( ! adSellList.isEmpty() )
		{
			MessageManager msgManager = new MessageManager( connection );
			int i = 0;
			
			for ( AdSell adSell : adSellList )
			{
				msgManager.sendMessage( adSell.getLoginSeller(), this.userId,
						this.adBuy.getId(), "Mise en correspondance automatique",
						"Une offre de vente que vous avez posté, ayant pour intitulé: ''" +
						adSell.getTitle() + "'', correspond à une offre postée par le vendeur ''" +
						this.adBuy.getLoginBuyer() + "''.</br></br> Si cette annonce vous intéresse, "+
						"vous pouvez le contacter en répondant à ce message.", AdType.SELL );
				
				if( i < 5 )
				{
					msgManager.sendMessage( this.userId, adSell.getLoginSeller(),
						adSell.getId(), "Mise en correspondance automatique",
						"Une offre d'achat que vous avez posté, ayant pour intitulé: ''" +
						this.adBuy.getTitle() + "'', correspond à une offre postée par le vendeur ''" +
						adSell.getLoginSeller() + "''.</br></br> Si cette annonce vous intéresse, "+
						"vous pouvez le contacter en répondant à ce message.", AdType.BUY);
				}
				++i;
			}
		}
		
		try
		{
			connection.close();
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}
	}

	/**
	 * Sets the buy ad
	 *
	 * @param adBuy the AdBuy to set
	 */
	public void setAdBuy( AdBuy adBuy )
	{
		this.adBuy = adBuy;
	}

	/**
	 * Returns the buy ad
	 *
	 * @return the adSell
	 */
	public AdBuy getAdBuy()
	{
		return this.adBuy;
	}

	/**
	 * Sets the user Id
	 *
	 * @param userId the userId to set
	 */
	public void setUserId( String userId )
	{
		this.userId = userId;
	}

	/**
	 * Returns the user Id
	 *
	 * @return the userId
	 */
	public String getUserId()
	{
		return this.userId;
	}
}
